package com.zy.order.controller;

import annotation.SwaggerMapping;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zy.order.client.StockClinet;
import com.zy.order.domain.Orders;
import com.zy.order.mapper.OrdersMapper;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 这是那头猪？
 * @version 1.0
 * @description: TODO
 * @date 2023/5/30 16:21
 */
@Slf4j
@RestController
@RefreshScope
@Api(tags = "order接口")
@SwaggerMapping
@RequestMapping("/order")
public class OrderController {

    @Resource
    private StockClinet stockClinet;

    @Resource
    private OrdersMapper ordersMapper;

    // @SentinelResource(value = "hello", blockHandler = "helloBlock")

    @ApiOperation("hello接口")
    @GlobalTransactional
    @GetMapping("/hello")
    public String hello(){
        String s = stockClinet.hello();
        log.info(s);
        System.out.println(1/0);
        Orders orders = new Orders();
        orders.setProductId(1);
        orders.setAmount(1);
        orders.setOrderTime(new Date());
        int i = ordersMapper.insert(orders);
        return i != 1 ? "下单失败" : "下单成功";
    }

    public String helloBlock(BlockException e){
        System.out.println(e.getMessage());
        return "流控";
    }

}
